package com.dhl.mediator;

import com.dhl.mediator.smarthouse.Alarm;
import com.dhl.mediator.smarthouse.CoffeeMachine;
import com.dhl.mediator.smarthouse.Colleague;
import com.dhl.mediator.smarthouse.TV;

import java.util.HashMap;

/**
 * @title: Mediator
 * @Author danghailiang
 * @Date: 2022/8/22 10:48
 * @Version 1.0
 */
public class Mediator {
    /**
     * 被调节者结合
     */
    private HashMap<String, Colleague> stringColleagueHashMap = new HashMap<>();
    private HashMap<String, String> stringStringHashMap = new HashMap<>();
    public void register(String name, Colleague colleague){
        stringColleagueHashMap.put(name, colleague);

        if(colleague instanceof Alarm){
            stringStringHashMap.put("Alarm",name);
        }else if(colleague instanceof TV){
            stringStringHashMap.put("TV",name);
        }else if(colleague instanceof CoffeeMachine){
            stringStringHashMap.put("CoffeeMachine",name);
        }
    }

    /**
     * 具体中介者的核心方法
     * 1.根据得到消息，完成对应任务
     * 2.中介者在这个方法，协调各个具体的同事对象，完成任务
     * @param stateChange
     * @param name
     */
    public void getMessage(int stateChange, String name) {

        Colleague colleague = stringColleagueHashMap.get(name);
        if(colleague instanceof Alarm){
            TV tv = (TV) stringColleagueHashMap.get(stringStringHashMap.get("TV"));
            if(stateChange == 0){
                // 开始煮咖啡
                ((CoffeeMachine)stringColleagueHashMap
                        .get(stringStringHashMap.
                                get("CoffeeMachine")))
                        .startCoffee();
                tv.openTV();
            }else if(stateChange == 1){
                tv.stopTV();
            }
        }else if(colleague instanceof TV){
            TV tv = (TV) stringColleagueHashMap.get(stringStringHashMap.get("TV"));
            if(stateChange == 0){
                tv.openTV();
            }else if(stateChange == 1){
                tv.stopTV();
            }
        }
    }
}
